const app = getApp();
Page({
  data: {
    card_number: "",
    showCard: false,
    userInfo: '',
    recovery_money: '',
    remarks: ''
  },


  //根据卡号去查询水卡信息 存在为充值操作，反之为开卡操作
  fetchUserCard(e) {
    let card_number = app.trimAll(e.detail.value);
    if (card_number !== "") {
      this.getWaterCard(card_number);
    }
  },


  //获取水卡数据
  getWaterCard(card_number, isLoading = true) {
    isLoading && wx.showLoading({
      mask: true
    })
    app.ajax({
      url: "Partner/WaterCard/getWaterCard",
      data: {
        card: card_number
      },
      success: res => {
        isLoading && wx.hideLoading();
        const source = res.data;
        if (source.code > 0) {
          this.setData({
            showCard: true,
            cardInfo: source.data,
            recovery_money: source.data.balance,
            remarks: '',
          })
        } else {
          const obj = {
            cardInfo: "",
            showCard: false,
          }
          if (this.data.cardInfo) {
            Object.assign(obj, {
              recovery_money: '',
              remarks: ''
            })
          }
          this.setData(obj)
        }
      },
      error: e => {
        isLoading && wx.hideLoading();
        app.showToast("网络错误");
      }
    })
  },


  //扫码操作
  scanCode() {
    wx.scanCode({
      success: res => {
        res.result && this.setData({
          card_number: app.formatCard(res.result)
        })
        const card_number = app.trimAll(res.result);
        if (card_number === "") {
          app.showToast("请扫描正确的水卡号");
        } else {
          this.getWaterCard(card_number);
        }
      },
      fail: error => {
        console.log(error)
      }
    })
  },


  handleInput(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [name]: e.detail.value
    })
  },

  handleSubmit() {
    wx.showModal({
      content: `是否将水卡[${app.trimAll(this.data.card_number)}]回收？`,
      success: res => {
        if (res.confirm) {
          if (this.data.recovery_money === "") {
            app.showToast("请输入回收金额");
            return;
          }
          wx.showLoading({
            mask: true
          });
          app.ajax({
            url: "Partner/WaterCard/recovery",
            data: {
              card_sn: app.trimAll(this.data.card_number),
              recovery_money: this.data.recovery_money,
              remarks: this.data.remarks
            },
            success: res => {
              wx.hideLoading();
              if (res.data.code > 0) {
                const eventChannel = this.getOpenerEventChannel()
                eventChannel.emit('updataRecycleData', {});
                app.showToast(res.data.message, "success");
                setTimeout(() => {
                  this.setData({
                    card_number: '',
                    cardInfo: '',
                    showCard: false,
                    recovery_money: '',
                    remarks: ''
                  })
                }, 1500)
              } else {
                app.showToast(res.data.message);
              }
            },
            error: e => {
              wx.hideLoading();
              app.showToast("网络错误");
            }
          })
        }
      },
      fail: error => {

      }
    })
  },


  handleInputCardNumber(e) {
    const value = e.detail.value;
    let data = {
      card_number: value,
    }
    if (value.trim() === "") {
      Object.assign(data, {
        cardInfo: "",
        showCard: false,
      })
      if (this.data.cardInfo) {
        Object.assign(data, {
          recovery_money: "",
          remarks: "",
        })
      }
      this.setData(data)
    } else {
      this.setData({
        card_number: app.formatCard(value)
      })
    }
  },
})